package net.xdclass.controller;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.annotation.RepeatSubmit;
import net.xdclass.constant.RedisKey;
import net.xdclass.controller.request.ConfirmOrderRequest;
import net.xdclass.controller.request.ProductOrderPageRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.ClientTypeEnum;
import net.xdclass.enums.ProductOrderPayTypeEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.service.ProductOrderService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/order/v1")
@Slf4j
public class ProductOrderController {

    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @GetMapping("token")
    public JsonData getOrderToken () {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        String token = CommonUtil.getStringNumRandom(32);

        String key = String.format(RedisKey.SUBMIT_ORDER_TOKEN_KEY, accountNo, token);

        //令牌有效时间是30分钟
        redisTemplate.opsForValue().set(key, String.valueOf(Thread.currentThread().getId()), 30, TimeUnit.MINUTES);

        return JsonData.buildSuccess(token);
    }



    @PostMapping("page")
    public JsonData page(@RequestBody ProductOrderPageRequest request){
        Map<String,Object> result = productOrderService.page(request);

        return JsonData.buildSuccess(result);
    }

    @GetMapping("query_state")
    public JsonData queryState(@RequestParam(value = "out_trade_no") String outTradeNo){
        String state = productOrderService.queryState(outTradeNo);
        return StringUtils.isBlank(state)?
                JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST):
                JsonData.buildSuccess(state);
    }

    @PostMapping("confirm")
    @RepeatSubmit(limitType = RepeatSubmit.Type.TOKEN)
    public void confirmOrder(@RequestBody ConfirmOrderRequest request, HttpServletResponse response){
        JsonData jsonData = productOrderService.confirmOrder(request);
        if (jsonData.getCode()==0) {
            String clientType = request.getClientType();
            String payType = request.getPayType();
            //支付宝支付
            if (ProductOrderPayTypeEnum.ALI_PAY.name().equals(payType)) {
                if (clientType.equals(ClientTypeEnum.PC.name())) {
                    CommonUtil.sendHtmlMessage(response, jsonData);
                } else if (clientType.equals(ClientTypeEnum.APP.name())) {

                } else if (clientType.equals(ClientTypeEnum.H5.name())) {

                }
            } else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equals(payType)) {
                //微信支付
                CommonUtil.sendJsonMessage(response, jsonData);
            }
        } else {
            log.error("创建订单失败{}", jsonData.toString());
            CommonUtil.sendJsonMessage(response, jsonData);
        }
    }


}
